<!--

/*
* Copyright (C) 2005-2013 University of Sydney
*
* Licensed under the GNU License, Version 3.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.gnu.org/licenses/gpl-3.0.txt
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

/**
* brief description of file
*
* @author      Tom Murtagh
* @author      Kim Jackson
* @author      Ian Johnson   <ian.johnson@sydney.edu.au>
* @author      Stephen White   <stephen.white@sydney.edu.au>
* @author      Artem Osmakov   <artem.osmakov@sydney.edu.au>
* @copyright   (C) 2005-2013 University of Sydney
* @link        http://Sydney.edu.au/Heurist
* @version     3.1.0
* @license     http://www.gnu.org/licenses/gpl-3.0.txt GNU License 3.0
* @package     Heurist academic knowledge management system
* @subpackage  !!!subpackagename for file such as Administration, Search, Edit, Application, Library
*/

-->
<!--

/**
 * filename, brief description, date of creation, by whom
 * @copyright (C) 2005-2010 University of Sydney Digital Innovation Unit.
 * @link: http://HeuristScholar.org
 * @license http://www.gnu.org/licenses/gpl-3.0.txt
 * @package Heurist academic knowledge management system
 * @todo
 **/

-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>temporal.js Tester
</title>
  <script type="text/javascript" src="../../common/js/temporalObjectLibrary.js"></script>
  <!--script type="text/javascript" src="../js/date_formatted.js"></script-->
  <!--script type="text/javascript" src="../js/date.js"></script-->
	<script type="text/javascript" src="temporalTestInputs.js"></script>
  <script type="text/javascript">
  		var displayDiv;
		function runSelectedTest () {
  			displayDiv = document.getElementById("test-div");
			var testSelect = document.getElementById("testList");
			switch (parseInt(testSelect.value)) {
				case 1:
					testCase1();
					break;
				case 2:
					testCase2();
					break;
				case 3:
					testCase3();
					break;
				case 4:
					testCase4();
					break;
				case 5:
					testCase5();
					break;
				case 6:
					testCase6();
					break;
				case 7:
					testCase7();
					break;
				case 8:
					testCase8();
					break;
				case 9:
					testCase9();
					break;
				default:
					runtest();
			}
		}

  		function runtest () {
  			displayDiv = document.getElementById("test-div");
  			var str = document.getElementById("teststring").value;
  			if (!str) {
  			//	str = "Nov 09 2009 22:56:51 GMT+1100";
				str = "10/04/1952 15:45:45 UTC+11";  // just make up a date to use
			}
			var myDate = new TDate(str);
		}

		var linefeed = "<br>";

		function displayDate (tDate) {
			if (!tDate) {
				throw "null Date passed into displayDate!";
			}
			//get values individually asd display string
			var temp = " Year = " + tDate.getYear() + linefeed;
			temp += " Month = " + tDate.getMonth() + linefeed;
			temp += " Day = " + tDate.getDay() + linefeed;
			temp += " Hour = " + tDate.getHours() + linefeed;
			temp += " Minute = " + tDate.getMinutes() + linefeed;
			temp += " Second = " + tDate.getSeconds() + linefeed;
			temp += " Millisecond = " + tDate.getMilliseconds() + linefeed;
			temp += " TimezoneOffset = " + tDate.getTimezoneOffset() + linefeed;
			temp += " toString = " + tDate.toString() + linefeed;
			temp += " toString special = " + tDate.toString("ddd, MMM dd y HH:mm:ss") + linefeed;
//			temp += " ISO Formatted String = " + tDate.toString("yyyy/MM/ddTHH:mm:ss zz") + linefeed;
//			temp += " JSON Formatted String = " + tDate.toJSONString() + linefeed;
			return temp;
		}

		function displayUTCDate (tDate) {
			return;
			if (!tDate) {
				throw "null Date passed into displayUTCDate!";
			}
			//get values individually asd display string
			var temp = " Year = " + tDate.getUTCFullYear() + linefeed;
			temp += " Month = " + tDate.getUTCMonth() + linefeed;
			temp += " Day = " + tDate.getUTCDate() + linefeed;
			temp += " Hour = " + tDate.getUTCHours() + linefeed;
			temp += " Minute = " + tDate.getUTCMinutes() + linefeed;
			temp += " Second = " + tDate.getUTCSeconds() + linefeed;
			temp += " Millisecond = " + tDate.getUTCMilliseconds() + linefeed;
			temp += " toString = " + tDate.toUTCString() + linefeed;
			temp += " ISO Formatted String = " + tDate.toUTCString("yyyy-MM-ddTHH:mm:ss zzz") + linefeed;
			return temp;
		}

		function appendDateResults (origStr, displayDiv, tDate) {
			displayDiv.innerHTML += "<p>  original string  = " + origStr + " </p>";
			displayDiv.innerHTML += "<p>" + displayDate(tDate) + "</p>";
			displayDiv.innerHTML += "<p> *********** UTC output ****************** " + linefeed +
									displayUTCDate(tDate) + "</p>";

		}
		function testCase1() { // Create Date from different input strings and check all output functions.
			//make a Tdate
			var str = document.getElementById("teststring").value;
			var tDate;
			displayDiv.innerHTML = "";
			if (!str) {
				var formats = [ ["1952-04-11 15:45:45 UTC+1000", " yyyy-MM-dd HH:mm:ss UTC+OFFSET "],
								["1532-11-15 9:05:45 +0400",	" yyyy-MM-dd HH:mm:ss +OFFSET "],
								["1952-11-15 10:05:45+1000",	" yyyy-MM-dd HH:mm:ss+OFFSET "] //,
	//							["152-11-15 10:05:45 +1000",	" yyy-MM-dd HH:mm:ss+OFFSET "],
	//							["1952/11/15", " yyyy/MM/dd "],
	//							["1952/11/15 05", " yyyy/MM/dd HH "],
	//							["1952/11/15 05:56", " yyyy/MM/dd HH:mm "],
	//							["1952/11/15 05 +09",  " yyyy/MM/dd HH +OFFSET "],
	//							["152/6/15",  " yyy/MM/dd "],
	//							["6/6/1500",  " dd/MM/yyyy "],
	//							["-1952/11/15 05", " yyy/MM/dd HH "]
								];


				for (var i = 0; i < formats.length; i++) {
					try {
						str = formats[i][0];
						tDate = TDate.parse(str);
						displayDiv.innerHTML += "<p> ############ FORMAT[" + i + "] = " + formats[i][1] + "############### </p>";
						appendDateResults(str, displayDiv, tDate);
					}
					catch(e) {
						displayDiv.innerHTML += "<p> !!!!!!! Exception !!!!!!" + e + linefeed +
												" str = " + str + "</p>";
					}
				}
			} else {
				try {
					tDate = TDate.parse(str);
					appendDateResults(str, displayDiv, tDate);
				}
				catch(e) {
					displayDiv.innerHTML = "<p> !!!!!!! Exception !!!!!!" + e + linefeed +
											" str = " + str + "</p>";
				}
			}
		}

		function testCase2() { // Make Date using parseExact.
			//make a Date
			var str = document.getElementById("teststring").value,
				frm = document.getElementById("formatstring").value;
			var tDate;
			displayDiv.innerHTML = "";
			if (!str) {
				var formats = [ ["1952-04-11T15:45:45 UTC+1000", "yyyy-MM-dd HH:mm:ss UTCz"],
								["1532-11-15T9:05:45 +0400",	"yyyy-MM-dd H:mm:ss z"],
								["1952-11-15T10:05:45+1000",	"yyyy-MM-dd HH:mm:ssz"] ,
								["152-11-15 10:05:45 +1000",	" yyy-MM-dd HH:mm:ss+OFFSET "],
								["1952/11/15", " yyyy/MM/dd "],
								["1952/11/15 05", " yyyy/MM/dd HH "],
								["1952/11/15 05:56", " yyyy/MM/dd HH:mm "],
								["1952/11/15 05 +09",  " yyyy/MM/dd HH +OFFSET "],
								["152/6/15",  "yyy/M/d"],
								["6/6/1500",  "d/M/yyyy"],
								["-1952/11/15T05", "yyyy/MM/dd HH"]
								];


				for (var i = 0; i < formats.length; i++) {
					try {
						str = formats[i][0];
						frm = formats[i][1];
						tDate = TDate.parse(str);
						displayDiv.innerHTML += "<p> ############ FORMAT[" + i + "] = " + frm + "############### </p>";
						appendDateResults(str, displayDiv, tDate);
					}
					catch(e) {
						displayDiv.innerHTML += "<p> !!!!!!! Exception !!!!!!" + e + linefeed +
												" str = " + str + " format = " + frm + "</p>";
					}
				}
			} else {
				try {
					tDate = TDate.parse(str);
					appendDateResults(str, displayDiv, tDate);
				}
				catch(e) {
					displayDiv.innerHTML = "<p> !!!!!!! Exception !!!!!!" + e + linefeed +
											" str = " + str + "</p>";
				}
			}
		}

		function testCase3() { // Test create temporal from string
			//get string
			var str = document.getElementById("teststring").value;
			if (!str) {
				str = "|VER=1|TYP=p|TPQ=1930-03-17T05:45|TAQ=1930-05-17T05:45|PDE=1930-07-17T05:45|PDL=1930-09-17T05:45|PRF=3|DET=2";
			}
			//Test for valid format
			try  {
				var temporal = new Temporal(str);
				displayDiv.innerHTML = "<p>" + str + "</p>";
				displayDiv.innerHTML +=	"<p> *********** temporal toString ****************** " + linefeed +
										temporal.toString() + "</p>";
			}
			catch(e) {
				displayDiv.innerHTML = "<p>" + str + "</p>";
				displayDiv.innerHTML += "<p> !!!!!!!!!!!!!!!!! Invalid Termporal string !!!!!!!!!!!!!!!!!!!!!!! </p>";
				displayDiv.innerHTML += "<p>" + e + "</p>";
			}
		}

		function testCase4() { // Illegal Dateformats
			//make a Date
			var str = document.getElementById("teststring").value;
			if (!str) {
				str = "-10009-08-12T15:46:12.5+04:30";
			}
			var tDate = new Date(str);
			displayDiv.innerHTML = "<p>" + displayDate(tDate) + "</p>";
			displayDiv.innerHTML = displayDiv.innerHTML +
							"<p> *********** UTC output ****************** " + linefeed +
							displayUTCDate(tDate) + "</p>";
		}

		function testCase5() { // Create Temporal All Types and Display
			var str = "";
			var temporal = new Temporal(str);
			displayDiv.innerHTML = 	"<p> *********** null Temporal ****************** " + linefeed +
									temporal + "</p>";

			str = "|VER=1|TYP=s|DAT=1952-1-14T15:45";
			temporal = new Temporal(str);
			displayDiv.innerHTML = displayDiv.innerHTML +
							"<p> *********** simple date '" + str + "' ****************** " + linefeed +
							temporal + "</p>";
			str = "1957-12-12";
			temporal = new Temporal(str);
			displayDiv.innerHTML = displayDiv.innerHTML +
							"<p> *********** date '" + str + "' ****************** " + linefeed +
							temporal + "</p>";
			temporal.setType("d");
			displayDiv.innerHTML = displayDiv.innerHTML +
							"<p> *********** reset type to simple date ****************** " + linefeed +
							temporal + "</p>";

			str = "|V1|f|-100-1-14|-99-12-12|-99-12-25|-98-12-01|1";
			temporal = new Temporal(str);
			displayDiv.innerHTML = displayDiv.innerHTML +
							"<p> *********** fuzzy probable '" + str + "' ****************** " + linefeed +
							temporal + "</p>";
			temporal.setType("f");
			displayDiv.innerHTML = displayDiv.innerHTML +
							"<p> *********** reset type to fuzzy probable ****************** " + linefeed +
							temporal + "</p>";

			str = "|V1|c14|-1400|0";
			temporal = new Temporal(str);
			displayDiv.innerHTML = displayDiv.innerHTML +
							"<p> *********** c14 date '" + str + "' ****************** " + linefeed +
							temporal + "</p>";
			temporal.setType("c14");
			displayDiv.innerHTML = displayDiv.innerHTML +
							"<p> *********** reset type to c14 date ****************** " + linefeed +
							temporal + "</p>";
	}

		function testCase6() { // Modify Temporal Change TDate from String
			//make a Date
			var str = document.getElementById("teststring").value;
			if (!str) {
				str = "-10009-08-12T15:46:12.5+04:30";
			}
			var tDate = new Date(str);
			displayDiv.innerHTML = "<p>" + displayDate(tDate) + "</p>";
			displayDiv.innerHTML = displayDiv.innerHTML +
							"<p> *********** UTC output ****************** " + linefeed +
							displayUTCDate(tDate) + "</p>";
		}

		function testCase7() { // Modify Temporal Change Type to Simple Date.
			//make a Date
			var str = document.getElementById("teststring").value;
			if (!str) {
				str = "-10009-08-12T15:46:12.5+04:30";
			}
			var tDate = new Date(str);
			displayDiv.innerHTML = "<p>" + displayDate(tDate) + "</p>";
			displayDiv.innerHTML = displayDiv.innerHTML +
							"<p> *********** UTC output ****************** " + linefeed +
							displayUTCDate(tDate) + "</p>";
		}
		function testCase8() { // Modify Temporal Change TDate from TDate
			//make a Date
			var str = document.getElementById("teststring").value;
			if (!str) {
				str = "-10009-08-12T15:46:12.5+04:30";
			}
			var tDate = new Date(str);
			displayDiv.innerHTML = "<p>" + displayDate(tDate) + "</p>";
			displayDiv.innerHTML = displayDiv.innerHTML +
							"<p> *********** UTC output ****************** " + linefeed +
							displayUTCDate(tDate) + "</p>";
		}
		function testCase9() { // Acceptance Test   -  test all input string variations
			//make a Date
			var str = document.getElementById("teststring").value;
			if (!str) {
				for (var i = 0; i < testInputs.length; i++) {
					try {
						msg = testInputs[i][0];
						str = testInputs[i][1];
						tDate = TDate.parse(str);
						displayDiv.innerHTML += "<p> ##################" + msg + "############### </p>";
						displayDiv.innerHTML += "<p> TDate parse of " + str + " = " + tDate.toString() + " </p>";
					}
					catch(e) {
						displayDiv.innerHTML += "<p> !!!!!!! Exception !!!!!!" + e + linefeed +
												" str = " + str + " for test = " + msg + "</p>";
					}
				}
			} else {
				try {
					tDate = TDate.parse(str);
					appendDateResults(str, displayDiv, tDate);
				}
				catch(e) {
					displayDiv.innerHTML = "<p> !!!!!!! Exception !!!!!!" + e + linefeed +
											" str = " + str + "</p>";
				}
			}
		}

  </script>
</head>
<body>
<select name="testList" id="testList" >
  <option value="1">Create Date From String</option>
  <option value="2">Create Date Using parseExact </option>
  <option value="3">Create Temporal from String</option>
  <option value="4">Illegal Dateformats </option>
  <option value="5">Create Temporal All Types and Display</option>
  <option value="6">Modify Temporal Change TDate from String</option>
  <option value="7">Modify Temporal Change Type to Simple Date</option>
  <option value="8">Modify Temporal Change TDate from TDate</option>
  <option value="9">Acceptence Test</option>
</select><br/>
<label for="teststring">Enter Test String</label>
<input type="text" name="teststring" id="teststring" size="200" maxlength="300"   title="enter teststring">
<label for="formatstring">Enter Format String</label>
<input type="text" name="formatstring" id="formatstring" size="200" maxlength="300"   title="enter format of teststring">
<input type="button" name="testButton" id="testButton" value="Run Test"   onclick="runSelectedTest();">
<div id="test-div"> </div>
</body>
</html>
